﻿using Game.Command;
using Game.Command.Internal;
using Game.Map;
using Game.Map.Extensions;
using Game.Objects;

namespace Game.Controller.Internal
{
    class CaptureStratagy
    {
        readonly IGameMap _GameMap;
        
        GameBuilding _GameBuilding;
        
        public CaptureStratagy(IGameMap map)
        {
            _GameMap = map;
        }
        
        public void AssignCommand(IMapField field)
        {
            if (field.VehicleX().Vehicle == null)
            {
                return;
            }

            var list = MapUtil.GetAdjacentFields(_GameMap, field.Point);
            foreach (var cursor in list)
            {
                var check = _GameMap.Fields[cursor.Point];
                
                bool filter = CheckFilter(check, field);
                if (filter)
                {
                    continue;
                }
                
                cursor.CommandX().Command = new CaptureCommand(
                    field.VehicleX().Vehicle, _GameBuilding);
                cursor.ViewX().GameImage = GameImage.Square;
            }
            
        }

        bool CheckFilter(IMapField check, IMapField field)
        {
            
            var o = check.ObjectX().GameObject;
            if (o == null)
            {
                return true;
            }
            if (o.Building == null)
            {
                return true;
            }
            if (o.Building.Owner == null)
            {
                return true;
            }
            if (o.Building.Owner.Name == field.VehicleX().Vehicle.Owner.Name)
            {
                return true;
            }

            _GameBuilding = o.Building as GameBuilding;
            return false;
        }

    }
    
}
